package com.app.biz.controller.formal;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateException;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.app.biz.domain.request.BizIndustrialValueAddedByCityRequest;
import com.app.common.annotation.Anonymous;
import com.app.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.app.common.annotation.Log;
import com.app.common.core.controller.BaseController;
import com.app.common.core.domain.AjaxResult;
import com.app.common.enums.BusinessType;
import com.app.biz.domain.formal.BizIndustrialValueAddedByCityFormal;
import com.app.biz.service.IBizIndustrialValueAddedByCityFormalService;
import com.app.common.utils.poi.ExcelUtil;
import com.app.common.core.page.TableDataInfo;

/**
 * 分市工业增加值正式Controller
 *
 * @author SangGuangCan
 * @date 2024-01-02
 */
@RestController
@RequestMapping("/biz/formal/city")
public class BizIndustrialValueAddedByCityFormalController extends BaseController
{
    @Autowired
    private IBizIndustrialValueAddedByCityFormalService bizIndustrialValueAddedByCityFormalService;

    /**
     * 查询分市工业增加值正式列表
     */
//    @PreAuthorize("@ss.hasPermi('biz:formal:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizIndustrialValueAddedByCityFormal bizIndustrialValueAddedByCityFormal)
    {
        String dates = bizIndustrialValueAddedByCityFormal.getDates();
        if(StringUtils.isNotBlank(dates)){
            DateTime datesTimes= DateUtil.parse(bizIndustrialValueAddedByCityFormal.getDates(),"yyyy年MM月");
            bizIndustrialValueAddedByCityFormal.setYearMonth(datesTimes);
        }else{
            throw new RuntimeException("未找到日期");
        }
        startPage();
        List<BizIndustrialValueAddedByCityFormal> list = bizIndustrialValueAddedByCityFormalService.selectBizIndustrialValueAddedByCityFormalList(bizIndustrialValueAddedByCityFormal);
        return getDataTable(list);
    }

    /**
     * 导出分市工业增加值正式列表
     */
//    @PreAuthorize("@ss.hasPermi('biz:formal:export')")
    @Log(title = "分市工业增加值正式", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizIndustrialValueAddedByCityFormal bizIndustrialValueAddedByCityFormal)
    {
        List<BizIndustrialValueAddedByCityFormal> list = bizIndustrialValueAddedByCityFormalService.selectBizIndustrialValueAddedByCityFormalList(bizIndustrialValueAddedByCityFormal);
        ExcelUtil<BizIndustrialValueAddedByCityFormal> util = new ExcelUtil<BizIndustrialValueAddedByCityFormal>(BizIndustrialValueAddedByCityFormal.class);
        util.exportExcel(response, list, "分市工业增加值正式数据");
    }

    /**
     * 获取分市工业增加值正式详细信息
     */
//    @PreAuthorize("@ss.hasPermi('biz:formal:query')")
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(bizIndustrialValueAddedByCityFormalService.selectBizIndustrialValueAddedByCityFormalByBaseId(baseId));
    }

    /**
     * 新增分市工业增加值正式
     */
//    @PreAuthorize("@ss.hasPermi('biz:formal:add')")
    @Log(title = "分市工业增加值正式", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizIndustrialValueAddedByCityFormal bizIndustrialValueAddedByCityFormal)
    {
        return toAjax(bizIndustrialValueAddedByCityFormalService.insertBizIndustrialValueAddedByCityFormal(bizIndustrialValueAddedByCityFormal));
    }

    /**
     * 修改分市工业增加值正式
     */
//    @PreAuthorize("@ss.hasPermi('biz:formal:edit')")
    @Log(title = "分市工业增加值正式", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizIndustrialValueAddedByCityFormal bizIndustrialValueAddedByCityFormal)
    {
        return toAjax(bizIndustrialValueAddedByCityFormalService.updateBizIndustrialValueAddedByCityFormal(bizIndustrialValueAddedByCityFormal));
    }

    /**
     * 删除分市工业增加值正式
     */
//    @PreAuthorize("@ss.hasPermi('biz:formal:remove')")
    @Log(title = "分市工业增加值正式", businessType = BusinessType.DELETE)
    @DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(bizIndustrialValueAddedByCityFormalService.deleteBizIndustrialValueAddedByCityFormalByBaseIds(baseIds));
    }

    @Anonymous
    @GetMapping("/getIndustrialValueAddedByCityList")
    public AjaxResult getIndustrialValueAddedByCityList(BizIndustrialValueAddedByCityRequest request){
        if(StringUtils.isBlank(request.getDates())){
            return AjaxResult.warn("没有获取到指定日期");
        }
        try{
            DateUtil.parse(request.getDates(),"yyyy年MM月");
            return AjaxResult.success(bizIndustrialValueAddedByCityFormalService.getIndustrialValueAddedByCityList(request));
        }catch (DateException de){
            logger.error("日期格式校验失败,传入日期:{},正确格式:{}",request.getDates(),"yyyy年MM月");
            return AjaxResult.error("日期校验失败");
        }catch (Exception e){
            logger.error("未知错误:{}",e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * <p>
     *     查询APP左上角的日期数据
     * </p>
     * @return 日期的集合
     */
    @Anonymous
    @GetMapping("/getUniqueDatesFromDatabase")
    public AjaxResult getUniqueDatesFromDatabase(){
        List<String> dateArgs = bizIndustrialValueAddedByCityFormalService.getUniqueDatesFromDatabase();
        return AjaxResult.success(dateArgs);
    }
}
